#include "Col.ph"
#ifdef __GNUG__
#pragma implementation
#endif

#include "FontEncoding.h"
#include "Class.h"
#include "String.h"
#include "ET_stdio.h"

#define CI(code,type,hex,name,uppercase,sort) { type, hex, name },
static CharInfo IsoInfo[]= {
#   include "IsoEncoding"
};
static CharInfo MacInfo[]= {
#   include "MacEncoding"
};
static CharInfo SymInfo[]= {
#   include "SymbolEncoding"
};
#undef CI

#define CI(code,type,hex,name,uppercase,sort) uppercase,
static u_char MacUppercaseMap[]= {
#   include "MacEncoding"
};
u_char IsoUppercaseMap[]= {
#   include "IsoEncoding"
};
#undef CI

#define CI(code,type,hex,name,uppercase,sort) sort,
static u_char MacSortMap[]= {
#   include "MacEncoding"
};
static u_char IsoSortMap[]= {
#   include "IsoEncoding"
};
#undef CI

static FontEncoding
	MacEncoding("Mac", 256, MacInfo, MacUppercaseMap, MacSortMap),
	IsoEncoding("Iso", 256, IsoInfo, IsoUppercaseMap, IsoSortMap),
	SymbolEncoding("Symbol", 256, SymInfo, identitymap, identitymap);

const FontEncoding
	*gStdEncoding= &MacEncoding,
	*gMacEncoding= &MacEncoding,
	*gISOEncoding= &IsoEncoding,
	*gSymbolEncoding= &SymbolEncoding;

SimpleMetaImpl(CharInfo)
{
	len= len;
	if (! isptr) {
		CharInfo *ci= (CharInfo*)addr;
		sprintf(buf, "%s %d %d", ci->name ? ci->name : ".notdef",
							ci->code, ci->hexval);
	}
}

//---- FontEncoding ------------------------------------------------------------

NewMetaImpl(FontEncoding,RefCounted, (TP(name), TV(info,len)));

FontEncoding::FontEncoding(const char *nm, int l, CharInfo ci[], u_char upper[],
								u_char sort[])
{
	name= strsave(nm);
	len= l;
	info= ci;
	lowercase= upcase= upper;
	sortmap= sort;
}

FontEncoding::~FontEncoding()
{
	SafeDelete(name);
	info= 0;
}

const char *FontEncoding::CodeToName(int code) const
{
	return info[code].name;
}

int FontEncoding::NameToCode(const char *nm) const
{
	register char *s;
	for (int i= 0; i < len; i++)
		if ((s= info[i].name) && strcmp(s, nm) == 0)
			return i;
	return -1;
}

const u_char *FontEncoding::SortMap() const
{
	return sortmap;
}

const u_char *FontEncoding::UpperCaseMap() const
{
	return upcase;
}

const u_char *FontEncoding::LowerCaseMap() const
{
	return lowercase;
}

